package com.sh.shanhuo.view;

import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;

/**
 * @author lcw
 * @date 2021/1/6
 * @desc
 */
public class ViewAnimUtils {

    public enum Anim {
        TOP_TO_BOTTOM,
        BOTTOM_TO_TOP,
        LEFT_TO_RIGHT,
        RIGHT_TO_LEFT
    }

    public static void setVisible(final View view, final Anim anim, final long durationMillis) {
        if (view == null) {
            return;
        }
        if (view.isShown()) {
            view.setVisibility(View.VISIBLE);
            return;
        }
        if (durationMillis == 0) {
            view.setVisibility(View.VISIBLE);
            return;
        }
        view.setVisibility(View.VISIBLE);
        TranslateAnimation translate = null;
        switch (anim) {
            case TOP_TO_BOTTOM:
                translate = new TranslateAnimation(
                        TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                        TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                        TranslateAnimation.RELATIVE_TO_SELF, -1f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0f);
                break;
            case BOTTOM_TO_TOP:
                translate = new TranslateAnimation(
                        TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                        TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                        TranslateAnimation.RELATIVE_TO_SELF, 1f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0f);
                break;
            case LEFT_TO_RIGHT:
                translate = new TranslateAnimation(
                        TranslateAnimation.RELATIVE_TO_PARENT, -1f,
                        TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0f);
                break;
            case RIGHT_TO_LEFT:
                translate = new TranslateAnimation(
                        TranslateAnimation.RELATIVE_TO_PARENT, 1f,
                        TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0f);
                break;
        }
        translate.setDuration(durationMillis);
        view.startAnimation(translate);
    }

    public static void setGone(final View view, final Anim anim, final long durationMillis) {
        if (view == null) {
            return;
        }
        if (!view.isShown()) {
            view.setVisibility(View.GONE);
            return;
        }
        if (durationMillis == 0) {
            view.setVisibility(View.GONE);
            return;
        }
        TranslateAnimation translate = null;
        switch (anim) {
            case TOP_TO_BOTTOM:
                translate = new TranslateAnimation(
                        TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                        TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0f,
                        TranslateAnimation.RELATIVE_TO_SELF, 1f);
                break;
            case BOTTOM_TO_TOP:
                translate = new TranslateAnimation(
                        TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                        TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0f,
                        TranslateAnimation.RELATIVE_TO_SELF, -1f);
                break;
            case LEFT_TO_RIGHT:
                translate = new TranslateAnimation(
                        TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                        TranslateAnimation.RELATIVE_TO_PARENT, 1f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0f);
                break;
            case RIGHT_TO_LEFT:
                translate = new TranslateAnimation(
                        TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                        TranslateAnimation.RELATIVE_TO_PARENT, -1f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0f);
                break;
        }
        translate.setDuration(durationMillis);
        view.startAnimation(translate);
        translate.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    /**
     * 开启View闪烁效果
     *
     * @param view
     */
    public static void startFlick(View view) {
        if (null == view) {
            return;
        }
        Animation alphaAnimation = new AlphaAnimation(1, 0.1f);
        alphaAnimation.setDuration(1000);
        alphaAnimation.setInterpolator(new LinearInterpolator());
        alphaAnimation.setRepeatCount(Animation.INFINITE);
        alphaAnimation.setRepeatMode(Animation.REVERSE);

        view.startAnimation(alphaAnimation);
    }

}
